package com.isoft.jms;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
/**
 * 配置类，创建两种消息模式目标，并配置ListenerContainer，实现同时使用topic和queue
 * @Author yigeyanse@hotmail.com
 * @Date 2017年9月6日 上午9:04:08
 */
@Configuration
public class JmsConfig {

	public final static String TOPIC = "jms.topic";
	public final static String QUEUE = "jms.queue";

	/**
	 * 创建队列模式的目标
	 * @return
	 */
	@Bean
	public Queue queue() {
		return new ActiveMQQueue(QUEUE);
	}

	/**
	 * 创建主题模式的目标
	 * @return
	 */
	@Bean
	public Topic topic() {
		return new ActiveMQTopic(TOPIC);
	}

	/**
	 * topic模式的ListenerContainer
	 * @param activeMQConnectionFactory
	 * @return
	 */
	@Bean
	public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
		DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
		bean.setPubSubDomain(true);
		bean.setConnectionFactory(activeMQConnectionFactory);
		return bean;
	}

	/**
	 * queue模式的ListenerContainer
	 * @param activeMQConnectionFactory
	 * @return
	 */
	@Bean
	public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory activeMQConnectionFactory) {
		DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
		bean.setConnectionFactory(activeMQConnectionFactory);
		return bean;
	}
}
